package com.homegoods.home.presenters;

import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.area.model.SubModelList;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.model.Constant;
import com.homegoods.home.HomeApiUtils;
import com.homegoods.home.model.AdModel;
import com.homegoods.home.model.HomeClassModel;
import com.homegoods.home.model.HomeGroupsModel;
import com.homegoods.home.model.NewProductModel;
import com.homegoods.home.views.IView;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.mvp.AbstractRefreshPresenter;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class HomeActivityFragementPresenter extends AbstractRefreshPresenter<IView> {

//    private HomeClassModel mChild;
    private HomeGroupsModel mChild;

    public HomeActivityFragementPresenter(IView mView) {
        super(mView);
        adapter.clear();
    }

    /**
     * 请求广告数据
     */
    public void requestAD(HomeGroupsModel child) {
        addSubscription(Observable.range(0, child.getBanners().size())
                .map(i -> HomeGoodsApp.getOSSHost() + "/" + child.getBanners().get(i).getImageUrl())
                .toList()
                .subscribe(strings -> mView.setAdPage(strings)
                ));

        refreshData();
    }

   /* public void requestAD(HomeClassModel child) {
        addSubscription(Observable.range(0, child.getData().getTopAD().size())
                .map(i -> Constant.SERVER_URL + child.getData().getTopAD().get(i).getImg())
                .toList()
                .subscribe(new Consumer<List<String>>() {
                               @Override
                               public void accept(List<String> strings) throws Exception {
                                   mView.setAdPage(strings);
                               }
                           }
                ));

        refreshData();

//        HomeApiUtils.getAdvertising(this::advertisingSuc, throwable -> {
//            throwable.printStackTrace();
//        });
    }*/


//    public void setMenuItems(HomeClassModel child) {
//        this.mChild = child;
//        mView.setMenuItems(this.mChild);
//    }

    public void setMenuItems(HomeGroupsModel child) {
        this.mChild = child;
        mView.setMenuItems(this.mChild);
    }

    /**
     * 商品详情
     * @param position
     */
    public void onTopAdClick(int position) {
//        AdModel adModel = mChild.getData().getTopAD().get(position);
        HomeGroupsModel.BannerBean bannerBean = mChild.getBanners().get(position);
//        String cmd = adModel.getCmd().substring(0, adModel.getCmd().indexOf("$"));
//        if (cmd.contains("SP")) {
            ARouter.getInstance()
                    .build(ARouterPath.PATH_GOODS_DETAIL)
                    .withString("sid", String.valueOf(bannerBean.getProductId()))
                    .navigation();
//        } else {
//            TODO
//        }
    }


    /**
     * 九宫格菜单点击
     *
     * @param position
     */
    public void onMenuClick(View view, int position) {
        HomeGroupsModel homeGroupsModel = (HomeGroupsModel) view.getTag();
        ARouter.getInstance()
                .build(ARouterPath.PATH_HOME_ACTIVITY_CLASSIFY)
                .withInt("position", position)
                .withString("title", homeGroupsModel.getChildren().get(position).getGroupName())
                .withSerializable("childlist", homeGroupsModel)
                .navigation();
    }


    @Override
    protected void fetchData(boolean isLoadMore) {
        requestLimitActivities();
    }

    private void requestLimitActivities() {
        HomeApiUtils.HomeComdiyList(1, 10, this::comdiyListSuc, throwable -> {
            mView.setRefreshing(false);
        });
    }

    /**
     * 获取新品发布成功
     */
    private void comdiyListSuc(SubModelList subModelList) {
        List<NewProductModel> list = JSON.parseArray(subModelList.getItems(), NewProductModel.class);
        if (list == null || list.size() <= 0) {
            mView.setRefreshing(false);
            return;
        }
        mView.setNewProductContent(list);
        mView.setRefreshing(false);

    }


}
